export * from './rem'

export * from './config'

export { debounce, throttle } from 'lodash'

export function pad(num, n = 2) {
  let len = num.toString().length
  while (len < n) {
    num = '0' + num
    len++
  }
  return num
}

export function formatDate(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
  date = date instanceof Date ? date : new Date(date)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      )
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}

export function formatTime(interval) {
  interval = interval | 0
  const minute = pad((interval / 60) | 0)
  const second = pad(interval % 60)
  return `${minute}:${second}`
}

export function formatNumber(number) {
  number = Number(number) || 0
  return number > 100000 ? `${Math.round(number / 10000)}万` : number
}

export function genImgUrl(url, w, h) {
  if (!h) {
    h = w
  }
  url += `?param=${w}y${h}`
  return url
}


export function isLast(index, arr) {
  return index === arr.length - 1
}

export function createSong(song) {
  return {
    id: song.id,
    name: song.name,
    artists: song.ar,
    alias: song.alia,
    duration: song.dt,
    artistsText: (song.ar || []).map(({ name }) => name).join('/'),
    durationSecond: song.dt / 1000,
    mvId: song.mv,
    albumName: song.al.name,
    img: song.al.picUrl
  }
}

export function songDetailParams(trackIds) {
  return {
    c: '[' + trackIds.map(id => '{"id":' + id + '}').join(',') + ']',
    ids: '[' + trackIds.join(',') + ']'
  }
}